/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.tms.entity.Unissued;
import com.jeeplus.modules.tms.mapper.UnissuedMapper;
import com.jeeplus.modules.tms.entity.UnissuedDetil;
import com.jeeplus.modules.tms.mapper.UnissuedDetilMapper;

/**
 * 未发量统计Service
 * @author 李超辉
 * @version 2018-05-29
 */
@Service
@Transactional(readOnly = true)
public class UnissuedService extends CrudService<UnissuedMapper, Unissued> {

	@Autowired
	private UnissuedDetilMapper unissuedDetilMapper;
	
	public Unissued get(String id) {
		Unissued unissued = super.get(id);
		unissued.setUnissuedDetilList(unissuedDetilMapper.findList(new UnissuedDetil(unissued)));
		return unissued;
	}
	
	public List<Unissued> findList(Unissued unissued) {
		return super.findList(unissued);
	}
	
	public Page<Unissued> findPage(Page<Unissued> page, Unissued unissued) {
		return super.findPage(page, unissued);
	}
	
	@Transactional(readOnly = false)
	public void save(Unissued unissued) {
		super.save(unissued);
		for (UnissuedDetil unissuedDetil : unissued.getUnissuedDetilList()){
			if (unissuedDetil.getId() == null){
				continue;
			}
			if (UnissuedDetil.DEL_FLAG_NORMAL.equals(unissuedDetil.getDelFlag())){
				if (StringUtils.isBlank(unissuedDetil.getId())){
					unissuedDetil.setUnissued(unissued);
					unissuedDetil.preInsert();
					unissuedDetilMapper.insert(unissuedDetil);
				}else{
					unissuedDetil.preUpdate();
					unissuedDetilMapper.update(unissuedDetil);
				}
			}else{
				unissuedDetilMapper.delete(unissuedDetil);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Unissued unissued) {
		super.delete(unissued);
		unissuedDetilMapper.delete(new UnissuedDetil(unissued));
	}
	
}